package com.kla.power.controller;

import com.kla.power.config.LogPointer;
import com.kla.power.entity.AlarmEntity;
import com.kla.power.entity.StationEntity;
import com.kla.power.entity.UserEntity;
import com.kla.power.service.IAlarmService;
import com.kla.power.service.IStationService;
import com.kla.power.util.RedisUtils;
import com.kla.power.util.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author: Wuzh
 * @Description:
 * @Date Created in 22:30 2024-06-10
 * @return
 */
@RequestMapping ( "dashboard" )
@RestController
@Slf4j
@CrossOrigin ( value = "*", maxAge = 3600 )
public class DashboardController {

    @Resource
    private IStationService iStationService;
    @Resource
    private IAlarmService iAlarmService;

    /**
     * 电站状态简报
     *
     * @return {
     * count": 9,               总电站
     * alarmStationCount": 1,   报警电站数量
     * allOffLineCount": 0,     全部设备离线电站数
     * join": 8,                接入设备电站数
     * partOffLineCount": 1     部分设备离线电站数
     * communicate: 1           通讯正常电站数=总电站-接入-(全部离线/部分离线)
     * }
     */
    @RequestMapping ( "stationStatus" )
    @LogPointer ( "数据看板：电站状态简报" )
    public ResultBody stationStatus ( @RequestBody StationEntity stationEntity ) {
        UserEntity userEntity = ( UserEntity ) RedisUtils.get ( stationEntity.getToken () );
        if ( userEntity == null ) {
            return ResultBody.fail ( "000600", "重新登陆" );
        }
        stationEntity.setMerchantId ( userEntity.getMerchantId () );
        Map<String, Integer> dashboard = iStationService.dashboardStationReport ( stationEntity );
        // 警报电站数量
        AlarmEntity alarmEntity = new AlarmEntity ();
        alarmEntity.setMerchantId ( userEntity.getMerchantId () );
        List<AlarmEntity> alarmEntities = iAlarmService.queryScreeAlarm ( alarmEntity );
        dashboard.put ( "alarmStationCount", alarmEntities.size () ); // 有报警电站数量
        Integer count = dashboard.get ( "count" ); // 总电站数
        Integer join = dashboard.get ( "join" );
        dashboard.put ( "communicate", ( count - join ) < 0 ? 0 : ( count - join ) );
        return ResultBody.success ( dashboard );
    }

    /**
     * 整体发电概况：当前商户下所有电站的发电概况
     *
     * @return
     */
    @RequestMapping ( "powerOverview" )
    @LogPointer ( "数据看板：整体发电概况" )
    public ResultBody powerOverview ( @RequestBody StationEntity stationEntity ) {
        UserEntity userEntity = ( UserEntity ) RedisUtils.get ( stationEntity.getToken () );
        if ( userEntity == null ) {
            return ResultBody.fail ( "000600", "重新登陆" );
        }
        stationEntity.setMerchantId ( userEntity.getMerchantId () );
        Calendar calendar = Calendar.getInstance ();
        stationEntity.setCreateTime ( calendar.getTime () );
        Map<String, Object> map = iStationService.dashboardStationPower ( stationEntity );
        return ResultBody.success ( map );
    }


    /**
     * 整体发电历史：
     * 1、某月总计发电量：2023-07-01 ~ 2023-07-31
     * 2、某年总计发电量：2023-01 ~ 2023-12
     *
     * @param stationEntity {
     *                      timeType: 1,
     *                      startTime: '2023-07-01',
     *                      endTime: '2023-07-31'
     *                      }
     *                      {
     *                      timeType: 2,
     *                      startTime: '2023-01-01',
     *                      endTime: '2023-12-31'
     *                      }
     * @return
     */
    @RequestMapping ( "allPowerHistory" )
    @LogPointer ( "数据看板：整体发电历史" )
    public ResultBody allPowerHistory ( @RequestBody StationEntity stationEntity ) {
        UserEntity userEntity = ( UserEntity ) RedisUtils.get ( stationEntity.getToken () );
        if ( userEntity == null ) {
            return ResultBody.fail ( "000600", "重新登陆" );
        }
        stationEntity.setMerchantId ( userEntity.getMerchantId () );
        Map<String, Double> map = iStationService.dashboardPowerHistory ( stationEntity );
        List<Map.Entry<String, Double>> listMap = new ArrayList<> ( map.entrySet () );
        Collections.sort ( listMap, new Comparator<Map.Entry<String, Double>> () {
            public int compare ( Map.Entry<String, Double> o1, Map.Entry<String, Double> o2 ) {
                return o1.getKey ().compareTo ( o2.getKey () );
            }
        } );
        return ResultBody.success ( listMap );
    }

    /**
     * 日满发小时排名：
     *
     * @param stationEntity
     * @return
     */
    @RequestMapping ( "dashboardFullHour" )
    @LogPointer ( "数据看板：日满发小时排名" )
    public ResultBody dashboardFullHour ( @RequestBody StationEntity stationEntity ) {
        UserEntity userEntity = ( UserEntity ) RedisUtils.get ( stationEntity.getToken () );
        stationEntity.setMerchantId ( userEntity.getMerchantId () );
        List<StationEntity> stationEntities = iStationService.dashboardFullHour ( stationEntity );
        return ResultBody.success ( stationEntities );
    }


    /**
     * 功率归一化排名
     *
     * @param stationEntity
     * @return
     */
    @RequestMapping ( "dashboardPower" )
    @LogPointer ( "数据看板：功率归一化排名" )
    public ResultBody dashboardPower ( @RequestBody StationEntity stationEntity ) {
        UserEntity userEntity = ( UserEntity ) RedisUtils.get ( stationEntity.getToken () );
        stationEntity.setMerchantId ( userEntity.getMerchantId () );
        List<StationEntity> stationEntities = iStationService.dashboardPower ( stationEntity );
        return ResultBody.success ( stationEntities );
    }

    /**
     * 电站分布，返回当前商户下所有电站列表，根据电站所在地点回显在地图上
     *
     * @param stationEntity
     * @return
     */
    @RequestMapping ( "stationDistribution" )
    @LogPointer ( "数据看板：电站分布" )
    public ResultBody stationDistribution ( @RequestBody StationEntity stationEntity ) {
        UserEntity userEntity = ( UserEntity ) RedisUtils.get ( stationEntity.getToken () );
        stationEntity.setType ( 1 );
        stationEntity.setMerchantId ( userEntity.getMerchantId () );
        List<StationEntity> stationEntities = iStationService.stationDistribution ( stationEntity );
        return ResultBody.success ( stationEntities );
    }

    /**
     * 已完成工单偏差排名：获取所有已完成工单，计算偏差取前十名
     *
     * @param taskEntity
     * @return
     */
    @RequestMapping ( "dashboardTaskDeviation" )
    @LogPointer ( "数据看板：已完成工单偏差排名" )
    public ResultBody dashboardTaskDeviation () {
        return ResultBody.success ();
    }


    /**
     * 工单状态简报
     *
     * @param taskEntity
     * @return
     */
    @RequestMapping ( "dashboardTaskStatus" )
    @LogPointer ( "数据看板：工单状态简报" )
    public ResultBody dashboardTaskStatus () {
        return ResultBody.success ();
    }

    /**
     * 未完成工单超时排名
     *
     * @param taskEntity
     * @return
     */
    @RequestMapping ( "dashboardTaskIncomplete" )
    @LogPointer ( "数据看板：未完成工单超时排名" )
    public ResultBody dashboardTaskIncomplete () {
        return ResultBody.success ();
    }

    /**
     * 工单处理人员排名,根据总评分排名
     *
     * @param taskEntity
     * @return
     */
    @RequestMapping ( "dashboardHandlerRanking" )
    @LogPointer ( "数据看板：工单处理人员排名" )
    public ResultBody dashboardHandlerRanking () {

        return ResultBody.success ();
    }

    /**
     * 新增工单趋势，查询当月工单量
     *
     * @param taskEntity
     * @return
     */
    @RequestMapping ( "dashboardAddTaskTrend" )
    @LogPointer ( "数据看板：新增工单趋势" )
    public ResultBody dashboardAddTaskTrend () {

        return ResultBody.success ();
    }

}
